Golang : Reclaim memory occupied by make() example
Problem :
You have allocated some memory for buffer via the make()
function and you want to reclaim back the occupied memory after done using the buffer. How to do that?
Solution :
For example :
buffer := make([]byte, 1024)
or
buffer := make([][]int, row)
to reclaim back the memory, simply do :
buffer = nil
Golang's garbage collector will automatically free up the memory or you can trigger garbage collection manually as well with runtime/debug.FreeOSMemory()
References :
https://www.socketloop.com/tutorials/golang-how-to-get-garbage-collection-data
https://www.socketloop.com/tutorials/golang-when-to-use-make-or-new
See also : Golang : When to use make or new?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8.8k Golang : Generate random Chinese, Japanese, Korean and other runes
+5.9k Golang : How to get capacity of a slice or array?
+10.1k Golang : Create matrix with Gonum Matrix package example
+12.1k Android Studio : Highlight ImageButton when pressed on example
+22.4k Golang : Read a file into an array or slice example
+5.8k Golang : Process non-XML/JSON formatted ASCII text file example
+8.1k Android Studio : Import third-party library or package into Gradle Scripts
+29.8k Get client IP Address in Go
+13.9k Golang : Recombine chunked files example
+9.6k Golang : Setting variable value with ldflags
+33.2k Golang : How to check if slice or array is empty?
+7.8k Golang : Routes multiplexer routing example with regular expression control